package until;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class OrderCoderUtil {
    /** 订单类别头 */
    private static final String DINGHUO = "DH-";
    /** 退货 */
    private static final String TUIHUO = "XT-";
    /**盘点 */
    private static final String PANDIAN = "PD-";
    /**调拨 */
    private static final String DIAOBO = "DB-";
    /** 收支流水 */
    private static final String SHOUZHI = "SZ-";
    /** 付款 */
    private static final String FUKUAN = "FK-";
    /** 收款 */
    private static final String SHOUKUAN = "SK-";
    /** 采购 */
    private static final String CAIGOU = "CG-";
    /** 采购退货 */
    private static final String CAIGOUTUIHUO = "CT-";
    /** 出库 */
    private static final String CHUKU = "CK-";
    /** 入库 */
    private static final String RUKU = "RK-";
    /** 用户编号 */
    private static final String YONGHU = "YH-";
    /** 随即编码 */
    private static final int[] r = new int[]{7, 9, 6, 2, 8 , 1, 3, 0, 5, 4};
    /** 用户id和随机数总长度 */
    private static final int maxLength = 10;



    public static String getByType(Integer type){
        switch (type){
            case 7:;
            case 8:;
            case 9:;
            case 22:;
            case 10:return getChuku(null);
            case 11:;
            case 12:;
            case 13:;
            case 21:;
            case 14:return getRuku(null);
        }
        return getShangPinCode(null);
    }
    /**
     * 生成调拨单号
     * @param userId
     */
    public static String getDiaobo(Long userId){
        return DIAOBO+getCode(userId);
    }
    /**
     * 生成出库单号
     * @param userId
     */
    public static String getChuku(Long userId){
        return CHUKU+getCode(userId);
    }
    /**
     * 生成入库单号
     * @param userId
     */
    public static String getRuku(Long userId){
        return RUKU+getCode(userId);
    }
    /**
     * 生成商品编号
     * @param userId
     */
    public static String getShangPinCode(Long userId){
        return getCode(userId);
    }
    /**
     * 生成订单单号编码
     * @param userId
     */
    public static String getDinghuoCode(Long userId){
        return DINGHUO + getCode(userId);
    }
    /**
     * 生成退货单号编码
     * @param userId
     */
    public static String getTuihuoCode(Long userId){
        return TUIHUO + getCode(userId);
    }
    /**
     * 生成盘点单号编码
     * @param userId
     */
    public static String getPandianCode(Long userId){
        return PANDIAN + getCode(userId);
    }
    /**
     * 生成收款单号编码
     * @param userId
     */
    public static String getShoukuanCode(Long userId){
        return SHOUKUAN + getCode(userId);
    }
    /**
     * 生成付款单号编码
     * @param userId
     */
    public static String getFukuanCode(Long userId){
        return FUKUAN + getCode(userId);
    }
    /**
     * 生成采购单号编码
     * @param userId
     */
    public static String getCaigouCode(Long userId){
        return CAIGOU + getCode(userId);
    }
    /**
     * 生成采购退货单号编码
     * @param userId
     */
    public static String getCaigoutuihuoCode(Long userId){
        return CAIGOUTUIHUO + getCode(userId);
    }
    /**
     * 生成收支流水单号编码
     * @param userId
     */
    public static String getShouzhiCode(Long userId){
        return SHOUZHI + getCode(userId);
    }
    /**
     * 生成用户编码
     * @param userId
     */
    public static String getYonghuCode(Long userId){
        return YONGHU + getCode(userId);
    }



    /**
     * 更具id进行加密+加随机数组成固定长度编码
     */
    private static String toCode(Long id) {
        String idStr = id.toString();
        StringBuilder idsbs = new StringBuilder();
        for (int i = idStr.length() - 1 ; i >= 0; i--) {
            idsbs.append(r[idStr.charAt(i)-'0']);
        }
        return idsbs.append(getRandom(maxLength - idStr.length())).toString();
    }

    /**
     * 生成时间戳
     */
    private static String getDateTime(){
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }

    /**
     * 生成固定长度随机码
     * @param n    长度
     */
    public static long getRandom(long n) {
        long min = 1,max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min ;
        return rangeLong;
    }

    /**
     * 生成不带类别标头的编码
     * @param userId
     */
    private static synchronized String getCode(Long userId){
        userId = userId == null ? 10000 : userId;
        return getDateTime() + toCode(userId);
    }
}
